/*
Audica Uploader - application for convenient uploading to various file hostings
Copyright (C) 2011-2012 Makarov Andrey (Winand)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "uploadform.h"
#include "ui_uploadform.h"
#include <QPainter>
#include <QDebug>
#ifdef Q_WS_X11
#include <QX11Info>
#endif

UploadForm::UploadForm(QWidget *parent) :
    QWidget(parent, Qt::Window | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint),
    ui(new Ui::UploadForm), dragging(false), drag_enabled(false)
{
    ui->setupUi(this);
#ifdef Q_WS_X11
    if(!QX11Info::isCompositingManagerRunning())
        pix_backgrd = QPixmap(":/opaque.png");
    else
#endif
    {
        pix_backgrd = QPixmap(":/transp" /*":/4.png"*/);
        setAttribute(Qt::WA_TranslucentBackground);
    }
    resize(pix_backgrd.width(), pix_backgrd.height());

    connect(ui->btnCancel, SIGNAL(confirmed()), this, SLOT(canceled()));
    connect(ui->btnLock, SIGNAL(clicked(bool)), this, SLOT(lock(bool)));
}

UploadForm::~UploadForm() {
    delete ui;
}

void UploadForm::setProgress(qint64 bytes, qint64 total) {
    //qDebug() << "setProgress" << bytes << total;
    if(total == -1) {
        ui->barProgress->setMaximum(0);
    } else {
        ui->barProgress->setMaximum(100);
        ui->barProgress->setValue((float)bytes / total * 100);
    }
}

void UploadForm::canceled() {
    qDebug() << "canceled";
    emit userCanceled();
}

void UploadForm::lock(bool checked) {
    drag_enabled = !checked;
}

void UploadForm::setButtonConfirmation(bool confirm) {
    ui->btnCancel->setConfirmation(confirm);
}

void UploadForm::setStatusText(const QString text) {
    ui->lblStatus->setText(text);
}

void UploadForm::setButtonText(const QString text) {
    ui->btnCancel->setText(text);
}

void UploadForm::paintEvent(QPaintEvent* event) {
    QPainter p(this);
    p.drawPixmap(rect(), pix_backgrd);
}

void UploadForm::mousePressEvent(QMouseEvent* event) {
    if(!drag_enabled) return;
    dragging = true;
    /*childRegion = childrenRegion();
    if(!childRegion.contains(event->pos()))*/ {
        if(event->button() == Qt::LeftButton) {
            dragPosition = event->globalPos() - frameGeometry().topLeft();
            event->accept();
        }
    }
}

void UploadForm::mouseReleaseEvent(QMouseEvent* event) {
    if(!drag_enabled) return;
    dragging = false;
}

void UploadForm::mouseMoveEvent(QMouseEvent* event) {
    if(!drag_enabled) return;
    if(event->buttons() & Qt::LeftButton && dragging) {
        move(event->globalPos() - dragPosition);
        event->accept();
    }
}
